import Tabx from "../basic/interfaces/Tabx";
import UITabItem from "../controls/UITabItem";
import * as Types from "../../types";
import * as CastHelper from "../utils/CastHelper";
import * as TypeHelper from "../utils/TypeHelper";
import * as ExceptionHelper from "../utils/ExceptionHelper";
import * as ElementHelper from "../utils/ElementHelper";
import * as Enums from "../basic/enums";
import UITabContent from "../controls/UITabContent";
import { exchangeValue } from "../utils/StringHelper";

export default class App {
    public readonly version: string = "1.0.0";
    public readonly prefix: string = "tabx-";

    private _item: UITabItem | null = null;
    get item() {
        return this._item;
    }
    set item(value: UITabItem | null) {
        this.tabx.item = this._item = value;
    }

    private _lastItem: UITabItem | null = null;
    get lastItem() {
        return this._lastItem;
    }
    set lastItem(value: UITabItem | null) {
        this.tabx.lastItem = this._lastItem = value;
    }

    private _items: Array<UITabItem> = [];
    get items() {
        return this._items;
    }

    constructor(private readonly tabx: Tabx) {
    }

    open(options: Types.TabOption) {
        const [itemContainer, contentContainer] = CastHelper.tupleStringOrElementCast(options.container);

        const itemContainerElement = typeof itemContainer === "string" ? document.querySelector(itemContainer) : itemContainer;
        const contentContainerElement = typeof contentContainer === "string" ? document.querySelector(contentContainer) : contentContainer;

        if (!itemContainerElement || !contentContainerElement) return;

        ElementHelper.addStyles(<HTMLElement>itemContainerElement, <Types.CSSStyleObject>{
            position: `relative`
        });

        ElementHelper.addStyles(<HTMLElement>contentContainerElement, <Types.CSSStyleObject>{
            position: `relative`
        });

        let item = this.getItem(options.item.id);
        if (!item) {
            item = new UITabItem(this, options.item);
            const tabItemElemnt = item.present();
            itemContainerElement.appendChild(tabItemElemnt);

            const uiContent = new UITabContent(this, item.id, item.url);
            const tabContentElement = uiContent.present();
            contentContainerElement.appendChild(tabContentElement);

            item.setComponent(Enums.ComponentType.TAB_CONTENT, uiContent);
            item.content = uiContent;
            this.items.push(item);
        }
        item.switch();
    }

    getItem(id: string): UITabItem | null {
        if (!TypeHelper.isStringWithNotEmpty(id)) ExceptionHelper.assertId();

        for (const item of this.items) {
            if (item.id === id) {
                if (item.element) {
                    return item;
                }
                else {
                    const index = this.items.indexOf(item);
                    this.items.splice(index, 1);
                    return null;
                }
            }
        }
        return null;
    }
}